package com.zyy.smartcity1129.movie;

import android.view.View;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieOrderInfo;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: MovieOrderFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 24/11/2021$
 * @Version: 1.0
 */
public class MovieOrderFragment extends BaseFragment {
    ExecutorService executor = Executors.newFixedThreadPool(1);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    AtomicInteger mInteger = new AtomicInteger(0);
    private RecyclerView rvMovieOrder;
    List<MovieOrderInfo.RowsBean> orderList = new ArrayList<>();
    RvAd<MovieOrderInfo.RowsBean> rvAd;

    @Override
    protected int layId() {
        return R.layout.fragment_movie_order;
    }

    @Override
    protected String title() {
        return "订单列表";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        initView(v);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                orderList = Http.getInstance().get("/prod-api/api/movie/ticket/order/list", null, MovieOrderInfo.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
        mData.observe(this, atomicInteger -> {
            if (mInteger.get() == 1) {
                initAdapter(orderList);
                rvMovieOrder.setLayoutManager(new LinearLayoutManager(getContext()));
                rvMovieOrder.setAdapter(rvAd);
            }
        });

    }

    private void initAdapter(List<MovieOrderInfo.RowsBean> mOrderList) {
        rvAd = new RvAd<MovieOrderInfo.RowsBean>(mOrderList, R.layout.rv_order) {
            private TextView tvRvOrder1, tvRvOrder2, tvRvOrder3, tvRvOrder4, tvRvOrder5, tvRvOrder6,
                    tvRvOrder7, tvRvOrder8, tvRvOrder9, tvRvOrder10;
            private void initView(View v) {
                tvRvOrder1 = (TextView) v.findViewById(R.id.tvRvOrder1);
                tvRvOrder2 = (TextView) v.findViewById(R.id.tvRvOrder2);
                tvRvOrder3 = (TextView) v.findViewById(R.id.tvRvOrder3);
                tvRvOrder4 = (TextView) v.findViewById(R.id.tvRvOrder4);
                tvRvOrder5 = (TextView) v.findViewById(R.id.tvRvOrder5);
                tvRvOrder6 = (TextView) v.findViewById(R.id.tvRvOrder6);
                tvRvOrder7 = (TextView) v.findViewById(R.id.tvRvOrder7);
                tvRvOrder9 = (TextView) v.findViewById(R.id.tvRvOrder9);
                tvRvOrder8 = (TextView) v.findViewById(R.id.tvRvOrder8);
                tvRvOrder10 = (TextView) v.findViewById(R.id.tvRvOrder10);
            }
            @Override
            protected void initR(View iv, MovieOrderInfo.RowsBean rowsBean, int p) {
                initView(iv);
                tvRvOrder1.setText(rowsBean.getMovieName());
                tvRvOrder2.setText(rowsBean.getTheatreName());
                tvRvOrder3.setText(rowsBean.getStartTime());
                tvRvOrder4.setText(rowsBean.getEndTime() + "散场");
                tvRvOrder5.setText(rowsBean.getRoomName());
                tvRvOrder6.setText("￥" + rowsBean.getPrice());
                tvRvOrder8.setText(rowsBean.getSeatRow() + "排" + rowsBean.getSeatCol() + "号");
                tvRvOrder10.setText("创建时间：" + rowsBean.getCreateTime());
                if (rowsBean.getStatus().equals("N")) {
                    tvRvOrder7.setText("待支付");
                } else {
                    tvRvOrder7.setText("已支付");
                }
                tvRvOrder7.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String result = Http.getInstance().get("/prod-api/api/movie/ticket/order/pay/" + rowsBean.getId(), null, ResultInfo.class, 1).getMsg();
                                orderList = Http.getInstance().get("/prod-api/api/movie/ticket/order/list", null, MovieOrderInfo.class, 1).getRows();
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        initAdapter(orderList);
                                        rvMovieOrder.setAdapter(rvAd);
                                    }
                                });
                            }
                        }).start();
                    }
                });
                tvRvOrder9.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String result = Http.getInstance().get("/prod-api/api/movie/ticket/order/" + rowsBean.getId(), null, ResultInfo.class, 4).getMsg();
                                orderList = Http.getInstance().get("/prod-api/api/movie/ticket/order/list", null, MovieOrderInfo.class, 1).getRows();
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        initAdapter(orderList);
                                        rvMovieOrder.setAdapter(rvAd);
                                    }
                                });
                            }
                        }).start();
                    }
                });
            }
        };
    }

    private void initView(View v) {
        rvMovieOrder = (RecyclerView) v.findViewById(R.id.rvMovieOrder);
    }
}
